/**
 * 
 */
package com.cvlep.appbase.config;

import java.lang.reflect.Constructor;

import org.apache.commons.configuration.Configuration;

/**
 * @author Otupia
 * 
 */
public enum ConfigurationFileType {
	XML("xml", "org.apache.commons.configuration.XMLConfiguration"), PROPERTIES(
			"properties",
			"org.apache.commons.configuration.PropertiesConfiguration");

	private String postfix;
	private Class<Configuration> configClass;

	private ConfigurationFileType(String postfix, String clazzName) {
		this.postfix = postfix;

		try {
			Class<Configuration> clazz;
			clazz = (Class<Configuration>) Class.forName(clazzName);
			this.configClass = clazz;
		} catch (ClassNotFoundException e) {
			System.out.println("Error in init enum ConfigurationFileType !!");
			System.out.println("No class found as : " + clazzName);
			e.printStackTrace();
		}
	}

	public Configuration getConfig(String filePath) {
		System.out.println(filePath);
		Configuration config = null;
		try {
			Constructor<Configuration> cofigConstructor = this.configClass
					.getConstructor(String.class);
			config = (Configuration) cofigConstructor.newInstance(filePath);
		} catch (Exception e) {
			throw new LoadConfigException("Fail to load config file : "
					+ filePath, e);

		}

		return config;
	}
}
